import { request } from '../../request/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        id: 0,
        value: '综合',
        isActive: true
      },
      {
        id: 1,
        value: '销量',
        isActive: false
      },
      {
        id: 2,
        value: '价格',
        isActive: false
      }
    ],
    goodList: []
  },
  QueryParams: {
    query: '',
    cid: '',
    pagenum: 1,
    pagesize: 10
  },
  totalPage: 1,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.QueryParams.cid = options.cid
    this.getGoodsList()
  },
  handelTabsItemChange(e) {
    const { index } = e.detail
    let { tabs } = this.data
    tabs.forEach((v, i) => {
      v.isActive = i === index ? true : false
    })
    this.setData({
      tabs
    })
  },
  async getGoodsList() {
    const { data: res } = await request({
      url: '/goods/search',
      data: this.QueryParams
    })
    if (res.meta.status !== 200) return
    this.totalPage = Math.ceil(res.message.total / this.QueryParams.pagesize)
    this.setData({
      goodList: [...this.data.goodList, ...res.message.goods]
    })
  },
  onReachBottom() {
    if (this.QueryParams.pagenum >= this.totalPage)
      wx.showToast({
        title: '没有更多数据了'
      })
    this.QueryParams.pagenum ++
    this.getGoodsList()

  },
  onPullDownRefresh() {
    this.setData({
      goodList: []
    })
    this.QueryParams.pagenum = 1
    this.getGoodsList()
    wx.stopPullDownRefresh()
  }
})